<?php
require ('../include/init.inc.php');
$method = $machine_id = $page_no='';
extract ( $_GET, EXTR_IF_EXISTS );

$start_date = trim($_GET['start_date']);
$end_date = trim($_GET['end_date']);
$search_val = trim($_GET['search_val']);

$row_count = Machine::getMachinesVisitListCount ();

/*如果是download方法，进行导出操作*/
if($method == "download"){
	$filename = '未拜访客户'.date('YmdHis');
	$header = array('客户编号','客户名称','联系人1','联系人2','联系电话','主机货号');
	$index = array('custom_code','customer_name','contacts_user','contacts_2','tel','host_number');
	
	$machineNoVister = Machine::getAllMachineByField();
	$machineVisterList = MachineVistor::getAllMachineVistorByField();
	
	$visterList = array();
	foreach ($machineVisterList as $key => $val){
		$visterList[$key] = $val["machine_id"];
	}
	/*匹配到拜访记录就将原来的机器数据剔除掉*/
	foreach ($machineNoVister as $k => $v){
		if(in_array($v["machine_id"], $visterList)){
			unset($machineNoVister[$k]);
		}
	}
	Machine::createtable($machineNoVister,$filename,$header,$index);
}

//START 数据库查询及分页数据
$page_size = PAGE_SIZE;
$page_no=$page_no<1?1:$page_no;

$total_page=$row_count%$page_size==0?$row_count/$page_size:ceil($row_count/$page_size);
$total_page=$total_page<1?1:$total_page;
$page_no=$page_no>($total_page)?($total_page):$page_no;

$start = ($page_no - 1) * $page_size;

$machines = Machine::getMachinesVisitList($start ,$page_size);
foreach ($machines as $k => &$machines_new){
// 	echo $machines_new["last_vistor_time"]."<br>";
    
	if($machines_new["last_vistor_time"] == "0"){
		$machines_new["tips"] = "无添加拜访记录";
		$machines_new["last_time"] = "--";
	}else{
		$zero1=strtotime (date("y-m-d h:i:s")); //当前时间  ,注意H 是24小时 h是12小时 
		$zero2=strtotime ($machines_new["last_vistor_time"]);  //过年时间，不能写2014-1-21 24:00:00  这样不对 
		$diff=ceil(($zero1-$zero2)/86400); //天 90天
		if($diff >=90){
			$machines_new["tips"] = "3个月未更新拜访记录";
			$machines_new["last_time"] = $machines_new["last_vistor_time"];
		}else{
			unset($machines[$k]);
		}  
	}
}



$page_html=Pagination::showPager("statistics_list.php?start_date=$start_date&end_date=$end_date&search_val=$search_val",$page_no,PAGE_SIZE,$row_count);
Template::assign ( 'page_no', $page_no );
Template::assign ( 'page_size', PAGE_SIZE );
Template::assign ( 'row_count', $row_count );
Template::assign ( 'page_html', $page_html );


Template::assign ( 'machines', $machines );

Template::display ( 'panel/statistics.tpl' );


